import { Body, Controller, Get, Param, ParseIntPipe, Post, Query } from '@nestjs/common'
import { ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger'
import { ApiSecurityAuth } from '~/common/decorators/swagger.decorator'
import { CreateOcrTaskDto } from '../dto/create-ocr-task.dto'
import { OcrQueryDto } from '../dto/ocr-query.dto'
import { OcrTaskEntity } from '../entities/ocr-task.entity'
import { OcrTaskResult, OcrTaskPaginationResult } from '../interfaces/ocr.interface'
import { OcrService } from '../services/ocr.service'

@ApiTags('Common - OCR文字识别服务')
@ApiSecurityAuth()
@Controller('common/ocr')
export class OcrController {
  constructor(private readonly ocrService: OcrService) {}

  @ApiOperation({ summary: '创建OCR文字识别任务' })
  @ApiResponse({ status: 200, description: '创建成功', type: Object })
  @Post('task')
  async createOcrTask(@Body() createOcrTaskDto: CreateOcrTaskDto): Promise<OcrTaskResult> {
    return this.ocrService.createTencentOcrTask(createOcrTaskDto)
  }

  @ApiOperation({ summary: '查询OCR任务列表' })
  @ApiResponse({ status: 200, description: '查询成功', type: Object })
  @Get('tasks')
  async findOcrTasks(@Query() query: OcrQueryDto): Promise<OcrTaskPaginationResult> {
    return this.ocrService.findAll(query)
  }

  @ApiOperation({ summary: '根据ID查询OCR任务' })
  @ApiParam({ name: 'id', description: 'OCR任务ID' })
  @ApiResponse({ status: 200, description: '查询成功', type: OcrTaskEntity })
  @Get('task/:id')
  async findOcrTaskById(@Param('id', ParseIntPipe) id: number): Promise<OcrTaskEntity> {
    return this.ocrService.findById(id)
  }

  @ApiOperation({ summary: '根据腾讯云任务ID查询OCR任务' })
  @ApiParam({ name: 'taskId', description: '腾讯云OCR任务ID' })
  @ApiResponse({ status: 200, description: '查询成功', type: OcrTaskEntity })
  @Get('task/by-task-id/:taskId')
  async findOcrTaskByTaskId(@Param('taskId') taskId: string): Promise<OcrTaskEntity> {
    return this.ocrService.findByTaskId(taskId)
  }
}
